package com.mlethe.android.demo.party;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.mlethe.android.demo.R;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class MainPartyActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";

    private String packageName;

    private static final String TN_URL_01 = "http://101.231.204.84:8091/sim/getacptn";

    int paySerial = 1;
    // 签名步骤建议不要在app上执行，要放在服务器上执行
    // appkey建议不要保存app
    final String APP_KEY = "d139ae6fb0175e5659dce2a7c1fe84d5";

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            String tn = (String) msg.obj;
            /*************************************************
             * 步骤2：通过银联工具类启动支付插件
             ************************************************/
            /*SocialTool.getUnionPay().setPayActionListener(MainPartyActivity.this)
//                    .pay(MainPartyActivity.this, "735315039351655905820");
                    .pay(tn);*/
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_party);
        Button alipaySesameCertificationBtn = findViewById(R.id.alipay_sesame_certification_btn);
        Button alipayLoginBtn = findViewById(R.id.alipay_login_btn);
        Button alipayPayBtn = findViewById(R.id.alipay_pay_btn);
        Button alipayShareTextBtn = findViewById(R.id.alipay_share_text_btn);
        Button alipayShareImgUrlBtn = findViewById(R.id.alipay_share_img_url_btn);
        Button alipayShareImgResBtn = findViewById(R.id.alipay_share_img_res_btn);
        Button alipayShareWebUrlBtn = findViewById(R.id.alipay_share_web_url_btn);
        Button alipayShareWebResBtn = findViewById(R.id.alipay_share_web_res_btn);
        Button wechatLoginBtn = findViewById(R.id.wechat_login_btn);
        Button wechatPayBtn = findViewById(R.id.wechat_pay_btn);
        Button wechatShareTextBtn = findViewById(R.id.wechat_share_text_btn);
        Button wechatShareImgUrlBtn = findViewById(R.id.wechat_share_img_url_btn);
        Button wechatShareImgResBtn = findViewById(R.id.wechat_share_img_res_btn);
        Button wechatShareWebUrlBtn = findViewById(R.id.wechat_share_web_url_btn);
        Button wechatShareWebResBtn = findViewById(R.id.wechat_share_web_res_btn);
        Button wechatShareMusicUrlBtn = findViewById(R.id.wechat_share_music_url_btn);
        Button wechatShareMusicResBtn = findViewById(R.id.wechat_share_music_res_btn);
        Button wechatShareVideoUrlBtn = findViewById(R.id.wechat_share_video_url_btn);
        Button wechatShareVideoResBtn = findViewById(R.id.wechat_share_video_res_btn);
        Button wechatShareProgramUrlBtn = findViewById(R.id.wechat_share_program_url_btn);
        Button wechatShareProgramResBtn = findViewById(R.id.wechat_share_program_res_btn);
        Button weiboLoginBtn = findViewById(R.id.weibo_login_btn);
        Button weiboShareTextBtn = findViewById(R.id.weibo_share_text_btn);
        Button weiboShareImgUrlBtn = findViewById(R.id.weibo_share_img_url_btn);
        Button weiboShareImgResBtn = findViewById(R.id.weibo_share_img_res_btn);
        Button weiboShareWebUrlBtn = findViewById(R.id.weibo_share_web_url_btn);
        Button weiboShareWebResBtn = findViewById(R.id.weibo_share_web_res_btn);
        Button weiboShareVideoBtn = findViewById(R.id.weibo_share_video_btn);
        Button weiboShareMultiImageBtn = findViewById(R.id.weibo_share_multi_image_btn);
        Button qqPayBtn = findViewById(R.id.qq_pay_btn);
        Button qqLoginBtn = findViewById(R.id.qq_login_btn);
        Button qqShareImgLocalBtn = findViewById(R.id.qq_share_img_local_btn);
        Button qqShareWebUrlBtn = findViewById(R.id.qq_share_web_url_btn);
        Button qqShareWebLocalBtn = findViewById(R.id.qq_share_web_local_btn);
        Button qqShareAudioUrlBtn = findViewById(R.id.qq_share_audio_url_btn);
        Button qqShareAudioLocalBtn = findViewById(R.id.qq_share_audio_local_btn);
        Button qqShareProgramUrlBtn = findViewById(R.id.qq_share_program_url_btn);
        Button qqShareProgramLocalBtn = findViewById(R.id.qq_share_program_local_btn);
        Button unionLoginBtn = findViewById(R.id.union_login_btn);
        Button unionPayBtn = findViewById(R.id.union_pay_btn);
        packageName = getApplicationInfo().packageName;
        if (packageName.equals("com.alipay.share.demo")) {
            // 支付宝
            alipaySesameCertificationBtn.setVisibility(View.VISIBLE);
            alipayLoginBtn.setVisibility(View.VISIBLE);
            alipayPayBtn.setVisibility(View.VISIBLE);
            alipayShareTextBtn.setVisibility(View.VISIBLE);
            alipayShareImgUrlBtn.setVisibility(View.VISIBLE);
            alipayShareImgResBtn.setVisibility(View.VISIBLE);
            alipayShareWebUrlBtn.setVisibility(View.VISIBLE);
            alipayShareWebResBtn.setVisibility(View.VISIBLE);
        } else if (packageName.equals("net.sourceforge.simcpux")) {
            // 微信
            wechatLoginBtn.setVisibility(View.VISIBLE);
            wechatPayBtn.setVisibility(View.VISIBLE);
            wechatShareTextBtn.setVisibility(View.VISIBLE);
            wechatShareImgUrlBtn.setVisibility(View.VISIBLE);
            wechatShareImgResBtn.setVisibility(View.VISIBLE);
            wechatShareWebUrlBtn.setVisibility(View.VISIBLE);
            wechatShareWebResBtn.setVisibility(View.VISIBLE);
            wechatShareMusicUrlBtn.setVisibility(View.VISIBLE);
            wechatShareMusicResBtn.setVisibility(View.VISIBLE);
            wechatShareVideoUrlBtn.setVisibility(View.VISIBLE);
            wechatShareVideoResBtn.setVisibility(View.VISIBLE);
            wechatShareProgramUrlBtn.setVisibility(View.VISIBLE);
            wechatShareProgramResBtn.setVisibility(View.VISIBLE);
        } else if (packageName.equals("com.sina.weibo.sdk.demo")) {
            // 微博
//            SocialTool.getInstance().initWeibo(this);
            weiboLoginBtn.setVisibility(View.VISIBLE);
            weiboShareTextBtn.setVisibility(View.VISIBLE);
            weiboShareImgUrlBtn.setVisibility(View.VISIBLE);
            weiboShareImgResBtn.setVisibility(View.VISIBLE);
            weiboShareWebUrlBtn.setVisibility(View.VISIBLE);
            weiboShareWebResBtn.setVisibility(View.VISIBLE);
            weiboShareVideoBtn.setVisibility(View.VISIBLE);
            weiboShareMultiImageBtn.setVisibility(View.VISIBLE);
        } else if (packageName.equals("com.tencent.sample")) {
            // QQ
            qqLoginBtn.setVisibility(View.VISIBLE);
            qqShareImgLocalBtn.setVisibility(View.VISIBLE);
            qqShareWebUrlBtn.setVisibility(View.VISIBLE);
            qqShareWebLocalBtn.setVisibility(View.VISIBLE);
            qqShareAudioUrlBtn.setVisibility(View.VISIBLE);
            qqShareAudioLocalBtn.setVisibility(View.VISIBLE);
            qqShareProgramUrlBtn.setVisibility(View.VISIBLE);
            qqShareProgramLocalBtn.setVisibility(View.VISIBLE);
        } else if (packageName.equals("com.pay.sample")) {
            // QQ
            qqPayBtn.setVisibility(View.VISIBLE);
        } else if (packageName.equals("com.enovatemotors.newretail")) {
            // 支付宝、微信、微博
//            SocialTool.getInstance().initWeibo(this);
            alipaySesameCertificationBtn.setVisibility(View.VISIBLE);
            alipayLoginBtn.setVisibility(View.VISIBLE);
            alipayPayBtn.setVisibility(View.VISIBLE);
            alipayShareTextBtn.setVisibility(View.VISIBLE);
            alipayShareImgUrlBtn.setVisibility(View.VISIBLE);
            alipayShareImgResBtn.setVisibility(View.VISIBLE);
            alipayShareWebUrlBtn.setVisibility(View.VISIBLE);
            alipayShareWebResBtn.setVisibility(View.VISIBLE);
            wechatLoginBtn.setVisibility(View.VISIBLE);
            wechatPayBtn.setVisibility(View.VISIBLE);
            wechatShareTextBtn.setVisibility(View.VISIBLE);
            wechatShareImgUrlBtn.setVisibility(View.VISIBLE);
            wechatShareImgResBtn.setVisibility(View.VISIBLE);
            wechatShareWebUrlBtn.setVisibility(View.VISIBLE);
            wechatShareWebResBtn.setVisibility(View.VISIBLE);
            wechatShareMusicUrlBtn.setVisibility(View.VISIBLE);
            wechatShareMusicResBtn.setVisibility(View.VISIBLE);
            wechatShareVideoUrlBtn.setVisibility(View.VISIBLE);
            wechatShareVideoResBtn.setVisibility(View.VISIBLE);
            wechatShareProgramUrlBtn.setVisibility(View.VISIBLE);
            wechatShareProgramResBtn.setVisibility(View.VISIBLE);
            weiboLoginBtn.setVisibility(View.VISIBLE);
            weiboShareTextBtn.setVisibility(View.VISIBLE);
            weiboShareImgUrlBtn.setVisibility(View.VISIBLE);
            weiboShareImgResBtn.setVisibility(View.VISIBLE);
            weiboShareWebUrlBtn.setVisibility(View.VISIBLE);
            weiboShareWebResBtn.setVisibility(View.VISIBLE);
            weiboShareVideoBtn.setVisibility(View.VISIBLE);
            weiboShareMultiImageBtn.setVisibility(View.VISIBLE);
            unionPayBtn.setVisibility(View.VISIBLE);
        } else {
            alipaySesameCertificationBtn.setVisibility(View.VISIBLE);
            alipayLoginBtn.setVisibility(View.VISIBLE);
            alipayPayBtn.setVisibility(View.VISIBLE);
            alipayShareTextBtn.setVisibility(View.VISIBLE);
            alipayShareImgUrlBtn.setVisibility(View.VISIBLE);
            alipayShareImgResBtn.setVisibility(View.VISIBLE);
            alipayShareWebUrlBtn.setVisibility(View.VISIBLE);
            alipayShareWebResBtn.setVisibility(View.VISIBLE);
            wechatLoginBtn.setVisibility(View.VISIBLE);
            wechatPayBtn.setVisibility(View.VISIBLE);
            wechatShareTextBtn.setVisibility(View.VISIBLE);
            wechatShareImgUrlBtn.setVisibility(View.VISIBLE);
            wechatShareImgResBtn.setVisibility(View.VISIBLE);
            wechatShareWebUrlBtn.setVisibility(View.VISIBLE);
            wechatShareWebResBtn.setVisibility(View.VISIBLE);
            wechatShareMusicUrlBtn.setVisibility(View.VISIBLE);
            wechatShareMusicResBtn.setVisibility(View.VISIBLE);
            wechatShareVideoUrlBtn.setVisibility(View.VISIBLE);
            wechatShareVideoResBtn.setVisibility(View.VISIBLE);
            wechatShareProgramUrlBtn.setVisibility(View.VISIBLE);
            wechatShareProgramResBtn.setVisibility(View.VISIBLE);
            weiboLoginBtn.setVisibility(View.VISIBLE);
            weiboShareTextBtn.setVisibility(View.VISIBLE);
            weiboShareImgUrlBtn.setVisibility(View.VISIBLE);
            weiboShareImgResBtn.setVisibility(View.VISIBLE);
            weiboShareWebUrlBtn.setVisibility(View.VISIBLE);
            weiboShareWebResBtn.setVisibility(View.VISIBLE);
            weiboShareVideoBtn.setVisibility(View.VISIBLE);
            weiboShareMultiImageBtn.setVisibility(View.VISIBLE);
            qqPayBtn.setVisibility(View.VISIBLE);
            qqLoginBtn.setVisibility(View.VISIBLE);
            qqShareImgLocalBtn.setVisibility(View.VISIBLE);
            qqShareWebUrlBtn.setVisibility(View.VISIBLE);
            qqShareWebLocalBtn.setVisibility(View.VISIBLE);
            qqShareAudioUrlBtn.setVisibility(View.VISIBLE);
            qqShareAudioLocalBtn.setVisibility(View.VISIBLE);
            qqShareProgramUrlBtn.setVisibility(View.VISIBLE);
            qqShareProgramLocalBtn.setVisibility(View.VISIBLE);
            unionLoginBtn.setVisibility(View.VISIBLE);
            unionPayBtn.setVisibility(View.VISIBLE);
        }
        alipaySesameCertificationBtn.setOnClickListener(this);
        alipayLoginBtn.setOnClickListener(this);
        alipayPayBtn.setOnClickListener(this);
        alipayShareTextBtn.setOnClickListener(this);
        alipayShareImgUrlBtn.setOnClickListener(this);
        alipayShareImgResBtn.setOnClickListener(this);
        alipayShareWebUrlBtn.setOnClickListener(this);
        alipayShareWebResBtn.setOnClickListener(this);
        wechatLoginBtn.setOnClickListener(this);
        wechatPayBtn.setOnClickListener(this);
        wechatShareTextBtn.setOnClickListener(this);
        wechatShareImgUrlBtn.setOnClickListener(this);
        wechatShareImgResBtn.setOnClickListener(this);
        wechatShareWebUrlBtn.setOnClickListener(this);
        wechatShareWebResBtn.setOnClickListener(this);
        wechatShareMusicUrlBtn.setOnClickListener(this);
        wechatShareMusicResBtn.setOnClickListener(this);
        wechatShareVideoUrlBtn.setOnClickListener(this);
        wechatShareVideoResBtn.setOnClickListener(this);
        wechatShareProgramUrlBtn.setOnClickListener(this);
        wechatShareProgramResBtn.setOnClickListener(this);
        weiboLoginBtn.setOnClickListener(this);
        weiboShareTextBtn.setOnClickListener(this);
        weiboShareImgUrlBtn.setOnClickListener(this);
        weiboShareImgResBtn.setOnClickListener(this);
        weiboShareWebUrlBtn.setOnClickListener(this);
        weiboShareWebResBtn.setOnClickListener(this);
        weiboShareVideoBtn.setOnClickListener(this);
        weiboShareMultiImageBtn.setOnClickListener(this);
        qqPayBtn.setOnClickListener(this);
        qqLoginBtn.setOnClickListener(this);
        qqShareImgLocalBtn.setOnClickListener(this);
        qqShareWebUrlBtn.setOnClickListener(this);
        qqShareWebLocalBtn.setOnClickListener(this);
        qqShareAudioUrlBtn.setOnClickListener(this);
        qqShareAudioLocalBtn.setOnClickListener(this);
        qqShareProgramUrlBtn.setOnClickListener(this);
        qqShareProgramLocalBtn.setOnClickListener(this);
        unionLoginBtn.setOnClickListener(this);
        unionPayBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        /*int id = view.getId();
        if (id == R.id.alipay_sesame_certification_btn) {
            Intent action = new Intent(Intent.ACTION_VIEW);
            String url = "https://openapi.alipaydev.com/gateway.do?alipay_sdk=alipay-sdk-java-3.7.4.ALL&app_id=2016101000649179&charset=utf-8&format=json&method=zhima.customer.certification.certify&sign=OQLDh10SyprbFlicgqwhrUK73F2pDkj0DhZdQnOnBQ%2FPDGpLXiRYOVXUbPtUr6MwgiLmR8vlNZCALqSx5%2Fcl81wdD6fQJ9vBZjUei%2Ffd%2Buu5eXqSj8RCK5qFMRE4zvFRrdBhuI8fXlsP%2FpTYxocddxRPlbFxh%2B2KmMpb3p%2BTrqmdyWbBsvvgjZfgtSOHn61%2FjLYge1Yndwj4pMW%2B%2FWUGeH6s9eOOQw3eu%2Bh7bm7na5ayLCN9l2aaCHkDhxulmBya8jaTxZ0vitJilPtVdCxVa%2B%2BfLdj%2F%2FV%2BmShW5L8oxzURRk5%2F%2BpwLXkL0COmuqAXEYZIxSc6gaOoBDuZc90bPpvg%3D%3D&sign_type=RSA2×tamp=2019-08-22+14%3A47%3A39&version=1.0";
            StringBuilder builder = new StringBuilder();
            builder.append("alipays://platformapi/startapp?appId=2016101000649179&url=");
//            builder.append(URLEncoder.encode(url));
            action.setData(Uri.parse(builder.toString()));
            startActivity(action);
        } else if (id == R.id.alipay_login_btn) {
            Log.e(TAG, "onClick: 登录");
            String oauth = "alipay_sdk=alipay-sdk-java-3.7.4.ALL&app_id=2016101000649179&biz_content=%7B++++++%22scopes%22%3A%5B++++++++%22auth_base%22++++++%5D%2C%22state%22%3A%22init%22%2C%22is_mobile%22%3A%22true%22++%7D&charset=GBK&format=json&method=alipay.user.info.auth&sign=VMH5gVZcQJXnUSdTCnq%2BoXKmnlqa9VEPqlUqzq4%2B9C1WJd4UIsWWXRot2ZWmzMY1rvksBIT8oWELQxdkLZWFXgIpXOuo304pd1xDHajMvMIz%2B847sNSxbgdQ43KQiCPEqLwnBQ9g%2BRIqXc9fBnIZzgPCjB2nbKve3dWyA9JMi6A8ppUTkJ4XQMpEu1f%2BGXYR3kH6Pvx5G8AV2yJWAwe54W9IrSnmeJYfCSB2W6Qh0sguj6pBCVo0fmW9I5hh%2FNvClMuH%2BZAeCRBSqLuwM1DmLwPxA7cK94Lh85icb6O%2BITEZXxWNAfij%2FV26PJ39orva48LCwgAUuYxilhHSZk3PHw%3D%3D&sign_type=RSA2×tamp=2019-08-22+16%3A58%3A22&version=1.0";
            final Map<String, String> bizParams = new HashMap<>();
            bizParams.put("url", ("https://authweb.alipay" +
                    ".com/auth?auth_type=PURE_OAUTH_SDK&app_id=")
                    .concat("2019080666104476")
//                        .concat("&scope=auth_user&state=init"));
                    .concat("&scope=auth_user,auth_base&state=init"));
            final String scheme = "com.enovatemotors.newretail";
            SocialTool.getAlipay().setOauthActionListener(this).oauth(this, oauth);
        } else if (id == R.id.alipay_pay_btn) {
            Log.e(TAG, "onClick: 支付");
            SocialTool.getAlipay().setPayActionListener(this).pay("https://qr.alipay.com/bax03816sgyopjpdbd0p4054");
        } else if (id == R.id.alipay_share_text_btn) {
            SocialTool.getInstance().shareAlipay(new ShareText("我是测试分享"), this);
        } else if (id == R.id.alipay_share_img_url_btn) {
            ShareImage image = new ShareImage("http://h.hiphotos.baidu.com/lvpics/w=600/sign=5dc3621e19d5ad6eaaf967eab1c939a3/0b55b319ebc4b745cc71eecccdfc1e178b821506.jpg");
            SocialTool.getInstance().shareAlipay(image, Share.FLAG_FRIEND, this);
        } else if (id == R.id.alipay_share_img_res_btn) {
            ShareImage image = new ShareImage(R.mipmap.ic_test_img);
            SocialTool.getInstance().shareAlipay(image, Share.FLAG_MOMENTS, this);
        } else if (id == R.id.alipay_share_web_url_btn) {
            ShareWeb web = new ShareWeb("https://www.baidu.com", "我是标题", "我是内容", "http://h.hiphotos.baidu.com/lvpics/w=600/sign=5dc3621e19d5ad6eaaf967eab1c939a3/0b55b319ebc4b745cc71eecccdfc1e178b821506.jpg");
            SocialTool.getInstance().shareAlipay(web, Share.FLAG_FRIEND, this);
        } else if (id == R.id.alipay_share_web_res_btn) {
            ShareWeb web = new ShareWeb("https://www.baidu.com", "我是标题", "我是内容", R.mipmap.ic_test_img);
            SocialTool.getInstance().shareAlipay(web, Share.FLAG_FRIEND, this);
        } else if (id == R.id.wechat_login_btn) {
            SocialTool.getWechat().setOauthActionListener(this).setGetInfoEnable(true).oauth();
        } else if (id == R.id.wechat_pay_btn) {
            wechatPay();
        } else if (id == R.id.wechat_share_text_btn) {
            SocialTool.getInstance().shareWechat(new ShareText("我是测试分享"), Share.FLAG_FRIEND, this);
        } else if (id == R.id.wechat_share_img_url_btn) {
            ShareImage image = new ShareImage("http://h.hiphotos.baidu.com/lvpics/w=600/sign=5dc3621e19d5ad6eaaf967eab1c939a3/0b55b319ebc4b745cc71eecccdfc1e178b821506.jpg");
            SocialTool.getInstance().shareWechat(image, Share.FLAG_FRIEND, this);
        } else if (id == R.id.wechat_share_img_res_btn) {
            ShareImage image = new ShareImage(R.mipmap.ic_test_img);
            SocialTool.getInstance().shareWechat(image, Share.FLAG_FRIEND, this);
        } else if (id == R.id.wechat_share_web_url_btn) {
            ShareWeb web = new ShareWeb("https://www.baidu.com", "我是标题", "我是内容", "http://h.hiphotos.baidu.com/lvpics/w=600/sign=5dc3621e19d5ad6eaaf967eab1c939a3/0b55b319ebc4b745cc71eecccdfc1e178b821506.jpg");
            SocialTool.getInstance().shareWechat(web, Share.FLAG_FRIEND, this);
        } else if (id == R.id.wechat_share_web_res_btn) {
            ShareWeb web = new ShareWeb("https://www.baidu.com", "我是标题", "我是内容", R.mipmap.ic_test_img);
            SocialTool.getInstance().shareWechat(web, Share.FLAG_FRIEND, this);
        } else if (id == R.id.wechat_share_music_url_btn) {
            ShareAudio audio = new ShareAudio("https://www.baidu.com", "http://hao.haolingsheng.com/ring/000/995/53142bfe578261cc624b0247baea084a.mp3", "我是标题", "我是内容", "http://h.hiphotos.baidu.com/lvpics/w=600/sign=5dc3621e19d5ad6eaaf967eab1c939a3/0b55b319ebc4b745cc71eecccdfc1e178b821506.jpg");
            SocialTool.getInstance().shareWechat(audio, Share.FLAG_FRIEND, this);
        } else if (id == R.id.wechat_share_music_res_btn) {
            ShareAudio audio = new ShareAudio("https://www.baidu.com", "http://hao.haolingsheng.com/ring/000/995/53142bfe578261cc624b0247baea084a.mp3", "我是标题", "我是内容", R.mipmap.ic_test_img);
            SocialTool.getInstance().shareWechat(audio, Share.FLAG_FRIEND, this);
        } else if (id == R.id.wechat_share_video_url_btn) {
            ShareVideo video = new ShareVideo("https://vdse.bdstatic.com//e22ca3cbcbb30eb2b39461602e8ff2ac?authorization=bce-auth-v1/fb297a5cc0fb434c971b8fa103e8dd7b/2017-05-11T09:02:31Z/-1//84df7fa5c7206bbd40a433165de2f3942759199e104bc6528701b115fa5d36c8", "我是标题", "我是内容", "http://h.hiphotos.baidu.com/lvpics/w=600/sign=5dc3621e19d5ad6eaaf967eab1c939a3/0b55b319ebc4b745cc71eecccdfc1e178b821506.jpg");
            SocialTool.getInstance().shareWechat(video, Share.FLAG_FRIEND, this);
        } else if (id == R.id.wechat_share_video_res_btn) {
            ShareVideo video = new ShareVideo("https://vdse.bdstatic.com//e22ca3cbcbb30eb2b39461602e8ff2ac?authorization=bce-auth-v1/fb297a5cc0fb434c971b8fa103e8dd7b/2017-05-11T09:02:31Z/-1//84df7fa5c7206bbd40a433165de2f3942759199e104bc6528701b115fa5d36c8", "我是标题", "我是内容", R.mipmap.ic_test_img);
            SocialTool.getInstance().shareWechat(video, Share.FLAG_FRIEND, this);
        } else if (id == R.id.wechat_share_program_url_btn) {
            ShareMiniProgram program = new ShareMiniProgram("http://www.qq.com", "gh_d43f693ca31f ", "/pages/media", 0, "我是标题", "我是内容", "http://h.hiphotos.baidu.com/lvpics/w=600/sign=5dc3621e19d5ad6eaaf967eab1c939a3/0b55b319ebc4b745cc71eecccdfc1e178b821506.jpg");
            SocialTool.getInstance().shareWechat(program, this);
        } else if (id == R.id.wechat_share_program_res_btn) {
            ShareMiniProgram program = new ShareMiniProgram("http://www.qq.com", "gh_d43f693ca31f ", "/pages/media", 0, "我是标题", "我是内容", R.mipmap.ic_test_img);
            SocialTool.getInstance().shareWechat(program, this);
        } else if (id == R.id.weibo_login_btn) {
            SocialTool.getWeibo().setGetInfoEnable(true).setOauthActionListener(this).oauth();
        } else if (id == R.id.weibo_share_text_btn) {
            SocialTool.getInstance().shareWeibo(new ShareText("我是测试分享"), this);
        } else if (id == R.id.weibo_share_img_url_btn) {
            ShareImage image = new ShareImage("http://h.hiphotos.baidu.com/lvpics/w=600/sign=5dc3621e19d5ad6eaaf967eab1c939a3/0b55b319ebc4b745cc71eecccdfc1e178b821506.jpg");
            SocialTool.getInstance().shareWeibo(image, this);
        } else if (id == R.id.weibo_share_img_res_btn) {
            ShareImage image = new ShareImage(R.mipmap.ic_test_img);
            SocialTool.getInstance().shareWeibo(image, this);
        } else if (id == R.id.weibo_share_web_url_btn) {
            ShareWeb web = new ShareWeb("https://www.baidu.com", "我是标题", "我是内容", "http://h.hiphotos.baidu.com/lvpics/w=600/sign=5dc3621e19d5ad6eaaf967eab1c939a3/0b55b319ebc4b745cc71eecccdfc1e178b821506.jpg");
            SocialTool.getInstance().shareWeibo(web, this);
        } else if (id == R.id.weibo_share_web_res_btn) {
            ShareWeb web = new ShareWeb("https://www.baidu.com", "我是标题", "我是内容", R.mipmap.ic_test_img);
            SocialTool.getInstance().shareWeibo(web, this);
        } else if (id == R.id.weibo_share_video_btn) {
            ShareVideo video = new ShareVideo("/storage/emulated/0/Pictures/Screenshots/SVID_20200110_165823_1.mp4", "我是内容");
            SocialTool.getInstance().shareWeibo(video, this);
        } else if (id == R.id.weibo_share_multi_image_btn) {
            List<String> list = new ArrayList<>();
            list.add("/storage/emulated/0/DCIM/Camera/IMG_20190810_174832.jpg");
            list.add("/storage/emulated/0/DCIM/Camera/IMG_20190810_164051.jpg");
            list.add("/storage/emulated/0/DCIM/Camera/IMG_20190810_164049.jpg");
            SocialTool.getInstance().shareWeibo(list, this, new ShareText("我是内容"));
        } else if (id == R.id.qq_pay_btn) {
            qqPay();
        } else if (id == R.id.qq_login_btn) {
            SocialTool.getQQ().setOauthActionListener(this).oauth(this);
        } else if (id == R.id.qq_share_img_local_btn) {
            ShareImage image = new ShareImage();
            image.setImagePath("/storage/emulated/0/DCIM/Camera/IMG_20190810_164049.jpg");
            SocialTool.getInstance().shareQQ(this, image, this);
        } else if (id == R.id.qq_share_web_url_btn) {
            ShareWeb web = new ShareWeb("https://www.baidu.com", "我是标题", "我是内容", "http://h.hiphotos.baidu.com/lvpics/w=600/sign=5dc3621e19d5ad6eaaf967eab1c939a3/0b55b319ebc4b745cc71eecccdfc1e178b821506.jpg");
            SocialTool.getInstance().shareQQ(this, web, this);
        } else if (id == R.id.qq_share_web_local_btn) {
            ShareWeb web = new ShareWeb("https://www.baidu.com", "我是标题", "我是内容");
            web.setImagePath("/storage/emulated/0/DCIM/Camera/IMG_20190810_164049.jpg");
            SocialTool.getInstance().shareQQ(this, web, this);
        } else if (id == R.id.qq_share_audio_url_btn) {
            ShareAudio audio = new ShareAudio("https://www.baidu.com", "http://hao.haolingsheng.com/ring/000/995/53142bfe578261cc624b0247baea084a.mp3", "我是标题", "我是内容", "http://h.hiphotos.baidu.com/lvpics/w=600/sign=5dc3621e19d5ad6eaaf967eab1c939a3/0b55b319ebc4b745cc71eecccdfc1e178b821506.jpg");
            SocialTool.getInstance().shareQQ(this, audio, this);
        } else if (id == R.id.qq_share_audio_local_btn) {
            ShareAudio audio = new ShareAudio("https://www.baidu.com", "http://hao.haolingsheng.com/ring/000/995/53142bfe578261cc624b0247baea084a.mp3", "我是标题", "我是内容");
            audio.setImagePath("/storage/emulated/0/DCIM/Camera/IMG_20190810_164049.jpg");
            SocialTool.getInstance().shareQQ(this, audio, this);
        } else if (id == R.id.qq_share_program_url_btn) {
            ShareMiniProgram program = new ShareMiniProgram("http://www.qq.com", "gh_d43f693ca31f ", "/pages/media", 0, "我是标题", "我是内容", "http://h.hiphotos.baidu.com/lvpics/w=600/sign=5dc3621e19d5ad6eaaf967eab1c939a3/0b55b319ebc4b745cc71eecccdfc1e178b821506.jpg");
            SocialTool.getInstance().shareQQ(this, program, this);
        } else if (id == R.id.qq_share_program_local_btn) {
            ShareMiniProgram program = new ShareMiniProgram("http://www.qq.com", "gh_d43f693ca31f ", "/pages/media", 0, "我是标题", "我是内容");
            program.setImagePath("/storage/emulated/0/DCIM/Camera/IMG_20190810_164049.jpg");
            SocialTool.getInstance().shareQQ(this, program, this);
        } else if (id == R.id.union_login_btn) {
            SocialTool.getUnionPay().setOauthActionListener(this).oauth(this, "upapi_user");
        } else if (id == R.id.union_pay_btn) {
            unionPay();
        }*/
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e(TAG, "onActivityResult: " + requestCode + "   data->" + data);
        if (data != null) {
            Bundle bundle = data.getExtras();
            for (String key : bundle.keySet()) {
                Log.e("Bundle Content", "Key=" + key + ", content=" + bundle.getString(key));
            }
        }
        /*if (packageName.equals("com.sina.weibo.sdk.demo")) {
            // 微博
            SocialTool.getWeibo().onActivityResult(requestCode, resultCode, data);
            SocialTool.getInstance().onActivityResult(data);
        } else if (packageName.equals("com.enovatemotors.newretail")) {
            // 支付宝、微信、微博、云闪付
            SocialTool.getWeibo().onActivityResult(requestCode, resultCode, data);
            SocialTool.getInstance().onActivityResult(data);
            SocialTool.getUnionPay().onActivityResult(requestCode, resultCode, data);
        } else if (packageName.equals("com.tencent.sample")) {
            // QQ
            SocialTool.getQQ().onActivityResult(requestCode, resultCode, data);
        } else {
            SocialTool.getUnionPay().onActivityResult(requestCode, resultCode, data);
            SocialTool.getWeibo().onActivityResult(requestCode, resultCode, data);
            SocialTool.getInstance().onActivityResult(data);
            SocialTool.getQQ().onActivityResult(requestCode, resultCode, data);
        }*/
    }

    public void onNotInstalled(int type) {
        Toast.makeText(this, "未安装APP", Toast.LENGTH_SHORT).show();
    }

    public void onComplete(int type) {
        Toast.makeText(this, "分享成功", Toast.LENGTH_SHORT).show();
    }

    public void onFail(int type) {
        Toast.makeText(this, "分享失败", Toast.LENGTH_SHORT).show();
    }

    public void onComplete(int type, JSONObject jsonObject) {
        Log.e(TAG, "onComplete: " + jsonObject.toString());
        Toast.makeText(this, "成功：" + jsonObject.toString(), Toast.LENGTH_SHORT).show();
    }

    public void onCancel(int type) {
        Toast.makeText(this, "分享取消", Toast.LENGTH_SHORT).show();
    }

    /**
     * 签名步骤建议不要在app上执行，要放在服务器上执行.
     */
    private String getSign(String appId, String tokenId, String pubAcc, String nonce, String bargainorId) throws Exception {
        // 按key排序
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("appId=").append(appId);
        stringBuilder.append("&bargainorId=").append(bargainorId);
        stringBuilder.append("&nonce=").append(nonce);
        stringBuilder.append("&pubAcc=").append(pubAcc);
        stringBuilder.append("&tokenId=").append(tokenId);

        byte[] byteKey = (APP_KEY+"&").getBytes("UTF-8");
        // 根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
        SecretKey secretKey = new SecretKeySpec(byteKey, "HmacSHA1");
        // 生成一个指定 Mac 算法 的 Mac 对象
        Mac mac = Mac.getInstance("HmacSHA1");
        // 用给定密钥初始化 Mac 对象
        mac.init(secretKey);
        byte[] byteSrc = stringBuilder.toString().getBytes("UTF-8");
        // 完成 Mac 操作
        byte[] dst = mac.doFinal(byteSrc);
        // Base64
        return Base64.encodeToString(dst, Base64.NO_WRAP);
    }

    /**
     * QQ 支付
     */
    private void qqPay() {
        new Thread(() -> {
            String getOrderNoUrl = "http://fun.svip.qq.com/mqqopenpay_demo.php";
            InputStream in;
            String httpResult = null;
            try {
                URL url = new URL(getOrderNoUrl);
                URLConnection urlConnection = url.openConnection();
                HttpURLConnection httpsConn = (HttpURLConnection) urlConnection;
                httpsConn.setAllowUserInteraction(false);
                httpsConn.setInstanceFollowRedirects(true);
                httpsConn.setRequestMethod("GET");
                httpsConn.connect();
                final int resCode = httpsConn.getResponseCode();
                final String resMsg = httpsConn.getResponseMessage();

                if (resCode == HttpURLConnection.HTTP_OK) {
                    in = httpsConn.getInputStream();

                    BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line);
                    }
                    in.close();
                    httpResult = sb.toString();
                    final JSONObject jsonObject = new JSONObject(httpResult);
                    int retCode = jsonObject.getInt("ret");
                    if (retCode == 0) {
                        // 支付
                        String appId = "100619284";
                        String serialNumber = "" + paySerial++;
                        String callbackScheme = "qwallet100619284";
                        String tokenId = jsonObject.getString("token");
                        String pubAcc = "";
                        String pubAccHint = "";
                        String nonce = String.valueOf(System.currentTimeMillis());
                        long timeStamp = System.currentTimeMillis() / 1000;
                        String bargainorId = "2001";
                        String sign = getSign(appId, tokenId, pubAcc, nonce, bargainorId);
//                        SocialTool.getQQ().setPayActionListener(this).pay(appId, serialNumber, callbackScheme, tokenId, pubAcc, pubAccHint, nonce, timeStamp, bargainorId, sign);
                    } else {
                        Log.e(TAG, "qqPay: retCode:" + retCode);
                        mHandler.post(() -> {
                            Toast.makeText(MainPartyActivity.this, "retCode:" + retCode, Toast.LENGTH_LONG).show();
                        });
                    }
                }
            } catch (final Exception e) {
                e.printStackTrace();
                mHandler.post(() -> {
                    Toast.makeText(MainPartyActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                });
            }
        }).start();
    }

    /**
     * 微信支付
     */
    private void wechatPay() {
        /*SocialTool.getWechat().setPayActionListener(this).pay(
                "wxc4c3b31e592dc8c2",
                "1523339171",
                "wx1912544231056440112b20081394401800",
                "1579409684000",
                1579409684,
                "Sign=WXPay",
                "C61B6784BF9BA76C427FFA22DDA06178",
                "");*/
    }

    /**
     * 云闪付支付
     */
    private void unionPay() {
        new Thread(() -> {
            String tn = null;
            InputStream is;
            try {

                String url = TN_URL_01;

                URL myURL = new URL(url);
                URLConnection ucon = myURL.openConnection();
                ucon.setConnectTimeout(120000);
                is = ucon.getInputStream();
                int i = -1;
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                while ((i = is.read()) != -1) {
                    baos.write(i);
                }

                tn = baos.toString();
                is.close();
                baos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            Message msg = mHandler.obtainMessage();
            msg.obj = tn;
            mHandler.sendMessage(msg);
        }).start();
    }
}
